import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { environment } from '@env/environment';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ShipStatService } from '../ship-stat.service';
import { BdOperationProcessEditComponent } from '../../basic-data/operation-process/edit/edit.component';
import { TstOriToDetailComponent } from '../tst-ori-to-detail/tst-ori-to-detail.component';
import { DragModalService } from '../../../../shared/utils/drag-modal.service';
import { AuthDictService } from '../../../system/dict/service/dict.service';
import { TallyManageUnlockService } from '../../tally-manage/unlock/tally-manage-unlock.service';
import { TallyCheckTstEditComponent } from '../../tally-check/check/tst/edit/edit.component';
import { TallyCheckOstEditComponent } from '../../tally-check/check/ost/edit/edit.component';
import { TallyCheckService } from '../../tally-check/check/tally-check.service';

@Component({
  selector: 'app-ship-stat-tally-list',
  templateUrl: './tally-list.component.html',
  styleUrls: ['./tally-list.component.less']
})
export class TallyListComponent implements OnInit {
	@Input()
	storageId: string;

	@Input()
	shipBillInfo: any;

	pageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	pageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};

	@ViewChild('st', { static: false }) private st!: STComponent;
	columns: STColumn[] = [
		{
			title: '日期班次',
			index: 'timeInfo',
			width: 80
		},
		{
			title: '航陆运',
			index: 'shipLandName',
			width: 60
		},
		{
			title: '理货方式',
			index: 'tallyTypeName',
			render: 'tallyTypeName',
			width: 60
		},
		{
			title: '作业过程',
			index: 'operationTypeName',
			width: 60
		},
		{
			title: '位置',
			index: 'positionInfo',
			width: 120
		},
		{
			title: '理货件吨',
			index: 'tallyPieceTonInfo',
			width: 100
		},
		{
			title: '检算件吨',
			index: 'checkPieceTonInfo',
			render: "checkPieceTonInfo",
			width: 100
		},
		{
			title: '理货员',
			index: 'createUserName',
			width: 60
		}

	];
	dataList = [];
	searchForm: FormGroup;
	userList = [];
	tallyTypeList = environment.dict.shipStatTallyTypeList;
	timeSpaceList = [];
	shipLandList = [];
	constructor(
		private fb: FormBuilder,
		private shipStatService: ShipStatService,
		private modalService: DragModalService,
		private dictService: AuthDictService,
		private checkService: TallyCheckService
	) {
		this.dictService.dictionary(environment.dict.SHIP_LAND, 1).subscribe(res => {
			this.shipLandList = res;
			this.shipLandList.push( { dictKey: 'TRANSFER', dictValue: '转配' } );
		});
		this.dictService.dictionary(environment.dict.TIME_SPACE, 1).subscribe(res => {
			this.timeSpaceList = res;
		});
		this.userList = this.dictService.tallyUserList;
		this.searchForm = this.fb.group({
			timeSpace: null,
			shipLand: null,
			tallyType: null,
			createUser: null
		});
	}

	ngOnInit(): void {
		this.loadData(this.storageId);

	}

	search(): any {
		this.loadData(this.storageId);
	}

	reset() {
		this.storageId = '';
		this.searchForm.reset();
		this.loadData(this.storageId);
	}


	loadData(storageId: string): any {
		const params = {
			storageId: storageId,
			transportBillId: this.shipBillInfo?.transportBillId,
			...this.searchForm.value,
			size: this.pageInfo.size,
			current: this.pageInfo.current
		};

		this.pageInfo.loading = true;
		this.shipStatService.shipStatTallyPage(params).subscribe((res: any) => {
			this.dataList = res.records;
			this.pageInfo.total = res.total;
			this.pageSetting.total = res.total;
			this.pageInfo.loading = false;
		});
	}

	stChange(ret: STChange) {
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.pageInfo.current = ret.pi;
			this.pageInfo.size = ret.ps;
			this.loadData(this.storageId);
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};

	showTstDetail(item: any) {
		this.modalService
			.createDrag({
				nzContent: TstOriToDetailComponent,
				nzComponentParams: { tallyId: item.tallyId },
				nzTitle: '杂作业/转配',
				nzFooter: null
			})
			.afterClose.subscribe(res => {
		});
	}

	export(transportBillId: string, storageId: string) {
		const params = {
			shipNameZh: this.shipBillInfo.shipNameZh,
			voyage: this.shipBillInfo.voyage,
			billNo: this.shipBillInfo.billNo,
			storageId: storageId,
			transportBillId: transportBillId,
			...this.searchForm.value,
		};
		window.open(`${environment.FINE_REPORT_URL}handle-tally/chuantongjilihuojilu.cpt&__parameters__=${JSON.stringify(params)}`, '_blank');
	}

	checkPieceTon(item: any) {
		if (item.dataFrom === 'ost') {
			this.checkService.ostShipStatDetail({id: item.tallyId}).subscribe(res => {
				this.modalService
					.createDrag({
						nzComponentParams: { record: res },
						nzWidth: '800px',
						nzContent: TallyCheckOstEditComponent,
						nzTitle: '修正',
						nzFooter: null
					})
					.afterClose.subscribe(res => {
					this.loadData(this.storageId);
				});
			})
		}
		if (item.dataFrom === 'tst') {
			this.checkService.tstShipStatDetail({id: item.tallyId}).subscribe(res => {
				this.modalService
					.createDrag({
						nzComponentParams: { record: res },
						nzWidth: '800px',
						nzContent: TallyCheckTstEditComponent,
						nzTitle: '修正',
						nzFooter: null
					})
					.afterClose.subscribe(res => {
					this.loadData(this.storageId);
				});
			})
		}


	}
}
